package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tvCity,tvWeather,tvTemp,tvWind,tvPm;
    private ImageView ivIcon;
    private List<WeatherInfo> infoList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        infoList =JsonParse.getInstance().getInfosFormJson(MainActivity.this);
        initView();
        getCityData("北京");
    }

    private void getCityData(String city) {
        for (WeatherInfo info:infoList){
            if (info.getCity().equals(city)){
                setData(info);
            }
        }
    }

    private void initView() {
        tvCity=(TextView)findViewById(R.id.tv_city);
        tvWeather=(TextView)findViewById(R.id.tv_weather);
        tvTemp=(TextView)findViewById(R.id.tv_temp);
        tvWind=(TextView)findViewById(R.id.tv_wind);
        tvPm=(TextView)findViewById(R.id.tv_pm);
        ivIcon=(ImageView) findViewById(R.id.iv_icon);
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
    }
    private void setData(WeatherInfo info){
        if (info==null)return;
        tvCity.setText(info.getCity());
        tvWeather.setText(info.getWeather());
        tvTemp.setText(info.getTemp());
        tvWind.setText("奋力:"+info.getWind());
        tvPm.setText("PM:"+info.getPm());
        if (("晴转多云").equals(info.getWeather())){
            ivIcon.setImageResource(R.drawable.ic_launcher_background);
        }else if (("多云").equals(info.getWeather())){
            ivIcon.setImageResource(R.drawable.ic_launcher_background);
        }else if (("晴").equals(info.getWeather())){
            ivIcon.setImageResource(R.drawable.ic_launcher_background);
        }

    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1:
                getCityData("北京");
                break;
            case R.id.button2:
                getCityData("上海");
                break;
            case R.id.button3:
                getCityData("广州");
                break;
        }
    }
}
